<script setup lang="ts">
import GaNavigationBar from "@/components/GaNavigationBar.vue";
import { DiaryUnitModel } from "@/model/DiaryUnitModel"
import { GlobalTool } from "@/utils/GlobalTool"
import router from "@/router"
import {
    GaRequestConfig,
    GaRequestMethod,
    GaRequestBodyInputType, GaResponseBodyOutputType, GaRequestManager, GaNetworkStatusCode,
    TwResultModel,
    GaMd5Manager
} from 'ga_utility_sdk_base';

let userId = GlobalTool.getCurrentUser().id;
if (!userId) {
    router.back();
}

function onInsert() {
    let model = new DiaryUnitModel();
    model.subject = "01-my_software_hierachy";
    model.content = "this is a very hard thing";
    model.diaryId = 1;
    model.categoryId = 1;
    model.parentId = 0;
    model.extra = "{}";
    model.userId = userId;
    let config = new GaRequestConfig(GaRequestMethod.POST, GaRequestBodyInputType.OBJ_2_JSON, GaResponseBodyOutputType.BLOB_2_JSONOBJ);
    config.requestBodyInput = model;
    config.apiPath = "DiaryUnit/Insert";
    GaRequestManager.sharedInstance().invokeRequest<DiaryUnitModel>(config, (status, res:TwResultModel<DiaryUnitModel>, msg, info) => {
        console.log(`info:${info}`)
        if (res) {
            let code = res.code;
            if (code == 0) {
                let model: DiaryUnitModel = res.resultData.item;
                console.log(`${JSON.stringify(model)}`)
                return;
            }
        }
    });
}
function onSearch() {
    let model = new DiaryUnitModel();
    model.parentId = 0;
    let userId = GlobalTool.getCurrentUser().id;
    if (!userId) {
        router.back();
        return;
    }
    model.userId = userId;
    let config = new GaRequestConfig(GaRequestMethod.POST, GaRequestBodyInputType.OBJ_2_JSON, GaResponseBodyOutputType.BLOB_2_JSONOBJ);
    config.requestBodyInput = model;
    config.apiPath = "DiaryUnit/Search";
    GaRequestManager.sharedInstance().invokeRequest<DiaryUnitModel[]>(config, (status, res: TwResultModel<DiaryUnitModel>, msg, info) => {
        console.log(`info:${info}`)
        if (res) {
            let code = res.code;
            if (code == 0) {
                let models: DiaryUnitModel[] = res.resultData.items;
                console.log(`${JSON.stringify(models)}`)
                return;
            }
        }

    });
}
function onUpdate() {
    let model = new DiaryUnitModel();
    model.id = 1;
    model.name = "0222-my_software_hierachy";
    model.parentId = 0;
    let userId = GlobalTool.getCurrentUser().id;
    if (!userId) {
        router.back();
        return;
    }
    model.userId = userId;
    let config = new GaRequestConfig(GaRequestMethod.POST, GaRequestBodyInputType.OBJ_2_JSON, GaResponseBodyOutputType.BLOB_2_JSONOBJ);
    config.requestBodyInput = model;
    config.apiPath = "DiaryUnit/Update";
    GaRequestManager.sharedInstance().invokeRequest<DiaryUnitModel>(config, (status, res:TwResultModel<DiaryUnitModel>, msg, info) => {
        console.log(`info:${info}`)
        if (res) {
            let code = res.code;
            if (code == 0) {
                let model: DiaryUnitModel = res.resultData.item;
                console.log(`${JSON.stringify(model)}`)
                return;
            }
        }
    });
}
function onDelete() {
    let model = new DiaryUnitModel();
    model.id = 1;
    let userId = GlobalTool.getCurrentUser().id;
    if (!userId) {
        router.back();
        return;
    }
    model.userId = userId;
    let config = new GaRequestConfig(GaRequestMethod.POST, GaRequestBodyInputType.OBJ_2_JSON, GaResponseBodyOutputType.BLOB_2_JSONOBJ);
    config.requestBodyInput = model;
    config.apiPath = "DiaryUnit/Delete";
    GaRequestManager.sharedInstance().invokeRequest<DiaryUnitModel>(config, (status, res, msg, info) => {
        console.log(`info:${info}`)
        if (res) {
            let code = res.code;
            if (code == 0) {
                console.log(`${JSON.stringify(model)}`)
                return;
            }
        }
        console.log(`status is: ${status},msg is: ${msg},info:${info}`)
    });
}
</script>

<template>
    <div id="container">
        <div id="content">
            <GaNavigationBar title="DiaryUnit"></GaNavigationBar>
            <el-row>
                Insert
                <el-button @click="onInsert">Insert</el-button>
            </el-row>
            <el-row>
                Search
                <el-button @click="onSearch">Search</el-button>
            </el-row>
            <el-row>
                Update
                <el-button @click="onUpdate">Update</el-button>
            </el-row>
            <el-row>
                Delete
                <el-button @click="onDelete">Delete</el-button>
            </el-row>
        </div>
    </div>
</template>

<style scoped>
#container {
    display: flex;
    align-items: center;
    /* background-color: gray; */
    justify-content: center;
}
</style>